package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.mapper.BrandMapper;
import com.spzx.product.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.stereotype.Service;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand>
        implements BrandService {

    @Override
    @Operation(summary = "校验品牌名称")
    public void checkBrandName(Brand brand) {
        if (brand.getId() == null) {
            brand.setCreateBy(SecurityUtils.getUsername());
        } else {
            brand.setUpdateBy(SecurityUtils.getUsername());
        }
        String name = brand.getName();
        Long count = baseMapper.selectCount(new LambdaQueryWrapper<Brand>().eq(Brand::getName, name));
        if (count > 0) {
            throw new ServiceException((brand.getId() == null ? "新增" : "修改") + "品牌名称已被占用");
        }
    }
}





